	var catalogId = GetQueryString("catalogId");
	var costId = GetQueryString("costId");
	
	var setting = {
		view: {
			showLine: false
		},
		data: {
			simpleData: {
				enable: true
			}
		}
	};
	//	获得节点 和 单位
	var allNodes;
	var shareUnit;
	
	$.getJSON("/ceca_two/router/shareUnit.json",function(rlt){
		shareUnit = rlt;
	})
		
	$.ajax({
		type:"post",
		url:"/guidance_service/front/costIndex/template/getByCatalogId.do",
		data:{
			catalogId:catalogId,
			type : GetQueryString("type")
		},success : function (result){
			
			if (result.data && result.data.templateContent) {
				
			}else{
				layer.msg("您选择的工程分类暂无模板" ,{icon:2,time:3000});
				return false;
			}
			
			var zNodes = JSON.parse(result.data.templateContent);
			var t = $.fn.zTree.init($("#treeDemo"), setting, zNodes);


			t.expandAll(true)
			allNodes = t.getNodes();

			
			for (i = 0 ; i < allNodes[0].children.length ; i++) {
				if (allNodes[0].children[i].pId == 0 || allNodes[0].children.pId == null) {
					if (i == 0) {
						$("#secondList").append("<li  data-id = '"+allNodes[0].children[i].id+"' onclick='draw("+i+",allNodes,this)'>"+allNodes[0].children[i].name+"</li>")
					}else{
						$("#secondList").append("<li  data-id = '"+allNodes[0].children[i].id+"' onclick='draw("+i+",allNodes,this)' >"+allNodes[0].children[i].name+"</li>")
					}
					$(".AllshareTable").append("<dl class='basementless'><dt class='basementlessName'><span>建筑部位</span><span>分部分项</span><span>技术参数</span><span>工程量</span><span>综合合价（万元）</span><span>操作</span></dt></dl>")
				}
			}
			
			draw(0,allNodes,"#secondList li:first")
			
		},error : function (){
			alert(2)
		}
	});




function draw (id,json,modern){

	$("#secondList li").removeClass("projectListOne");
	$(modern).addClass("projectListOne");
	
	$(".basementless").eq(id).show().siblings().hide();
	
	if ($(".basementless").eq(id).hasClass("finsh")) {

	} else{
		var data = json[0].children[id];
		for ( i = 0 ; i < data.children.length ; i++) {
	//		console.log(data.children[i])
			if (data.children[i].remake) {
//				console.log(data.children[i].remake)
				var html = "<dd class='basementlessText'><div class='name1' data-id="+data.children[i].id+" title='"+data.children[i].name+"'><span>"+data.children[i].name+"<img src = '/ceca_two/img/remake.png' title="+ClearBr(data.children[i].remake)+"  /></span></div>";
			}else{
				var html = "<dd class='basementlessText'><div class='name1' data-id="+data.children[i].id+" title='"+data.children[i].name+"'><span>"+data.children[i].name+"</span></div>";
			}

				html += "<div class= 'box hasBox' ><div class='leve'><div class='second'><ul class='name'  onclick='showList(this)'>不包含</ul><ul class='list'></ul></div>";
				//二级
				html += "<div class='third'></div>";
				//三级
				html += "<div class='fouth'></div></div>";
				//分部分项
				html += "<div class='para'><ul class='name' title=''></ul><ul class='inp'></ul></div>";
				//技术参数1
				html += "<div class='para'><ul class='name' title=''></ul><ul class='inp'></ul></div>";
				//技术参数2
				if (data.children[i].unit) {
					html += "<div class='eng'><ul class='name' title=''>"+chooseUnit(data.children[i].unit)+"</ul><ul class='inp'><input type='text' style='background:none' /></ul><ul class='danwei'><li onclick='selUnit(this)'>"+data.children[i].unit+"</li><dl></dl></ul></div>";
				} else{
					html += "<div class='eng'><ul class='name' title=''>长度</ul><ul class='inp'><input type='text' /></ul><ul class='danwei'><li onclick='selUnit(this)'>m</li><dl></dl></ul></div>";
				}
				//工程量
				html += "<div class='comp'><input type = 'text' class='price'  onclick='calculate(this)'/></div>";
				//综合合价
				html += "<div class='oper'><img src='/ceca_two/img/wang1.png' class='add1' onclick='add(this)'/><span style='flaot:left'>新增</span></div></div>";
				//操作
				html += "<div class='box hj' data-id="+data.children[i].id+"><li class='zhhj'>部位合价（万元）</li><li class='zhhjinput'><input type='text' class = 'onlyMoney'/></li></div></dd>"
				//综合合价
	//						<img src='/ceca_two/img/wang2.png'class='del1' onclick='del(this)' />
	//					$(".basementless").append(htmlStr);
			$(".basementless").eq(id).addClass("finsh").append(html);
			


			if (data.children[i].children) {
				
				for (j = 0 ; j < data.children[i].children.length ; j++) {
//						console.log(data.children[i].children)	
					$(".basementless").eq(id).find(".second").eq(i).find(".list").append("<li data-id="+data.children[i].children[j].id+" data-pid="+data.children[i].children[j].pId+" onclick='getList(this,2)'>"+data.children[i].children[j].name+"</li>")
					
				}
			}
		}
	}
}

function ClearBr(key) { 
	key = key.replace(/<\/?.+?>/g,""); 
	key = key.replace(/[\r\n]/g, ""); 
	return key; 
} 


function chooseUnit (thisUnit){
	var unitName ;
	for ( j = 0 ; j < shareUnit.length ; j ++) {
		thisUnit == shareUnit[j].unit ? unitName = shareUnit[j].name : "" ;
		
	}
	return unitName ;
}


function calculate(modern){

	$(modern).on("input propertychange",function (){
		var allValue = 0;
		for (i = 0 ; i < $(modern).parents(".basementlessText").find(".price").length ; i++) {
			
			allValue += Number($(modern).parents(".basementlessText").find(".price").eq(i).val())*10000
			
		}
		
		$(modern).parents(".basementlessText").find(".onlyMoney").val(allValue/10000)
		
	})
	
}


//	显示下拉列表
function showList(modern,e){
	if($(modern).next(".list").length > 0){
		$(modern).next(".list").toggle();
	
	//	增加其他选项	
		if ($(modern).next(".list").find(".otherLi").length == 0) {
			var otherPid = $(modern).next(".list").find("li").last().attr("data-pid");
			$(modern).next(".list").append("<li class='otherLi' data-pid = '"+otherPid+"' onclick = 'other(this)' >其他</li>")
		}
		window.event? window.event.cancelBubble = true : e.stopPropagation();
	}
	if($(modern).next(".list2").length > 0){
		$(modern).next(".list2").toggle();
		if ($(modern).next(".list2").find(".otherChoose").length == 0) {
			$(modern).next(".list2").append("<li class='optionBox' onclick='otherChoose(this)' title='其他'>其他</li>")
		}

		window.event? window.event.cancelBubble = true : e.stopPropagation();
	}
}

//分部分项select其他
function otherChoose (modern){
	$(modern).hide();
	$(modern).parents(".inp").find("div").first().empty().append("<input type='text' onclick='clearBg(this)' /><img src='/ceca_two/img/delete.png' onclick='delSelect(this)' class='otherImg'/>")
	$(modern).parents(".list2").hide();
}

function clearBg (modern){
	$(modern).css({"background":"none"})
	window.event? window.event.cancelBubble = true : e.stopPropagation();
//	$(modern).on("propertychange",function(){
//		$(modern).val() == "" ? $(modern).css({"background":" url('/ceca_two/img/enter.png') center center / 12px 12px no-repeat"}) : $(modern).css({"background":"none"});
//	})
}

function delSelect (modern) {
	$(modern).parent().empty().html("请选择")
	window.event? window.event.cancelBubble = true : e.stopPropagation();
}

//	点击其他选项	
function other (modern){

	
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	var thisPid = $(modern).attr("data-pid");
	var thisPNode = treeObj.getNodeByParam("id",thisPid);
//	console.log(thisPNode)
	var newId = thisPNode.children[thisPNode.children.length-1].id+1;

	var newNode = {
		id : Number(newId),
		pId : Number(thisPid),
		para : [{}]
	};
	
	treeObj.addNodes(thisPNode,newNode);
	
	$(modern).parent().parent().nextAll("div").empty();
	$(modern).parents(".hasBox").find(".para").find(".name").empty();
	$(modern).parents(".hasBox").find(".para").find(".inp").empty();
	$(modern).parents(".hasBox").find(".para").find(".danwei").empty();
	
	$(modern).parent().prev(".name").css("background","none")
	$(modern).parent().prev(".name").empty().append("<input type='text' data-pid='"+thisPid+"' data-id='"+newId+"' onblur = 'addData(this,1)' onclick='stopEvent()' class = 'addInput'  /><img src='/ceca_two/img/delete.png' class='otherImg' onclick='otherDel(this,1)' />")
	$(modern).parents(".hasBox").find(".para").eq(0).find(".name").append("<input type='text' data-pid='"+thisPid+"' data-id='"+newId+"' onblur = 'addData(this,2)' onclick='stopEvent() ' class = 'addInput'  /> ")
	$(modern).parents(".hasBox").find(".para").eq(0).find(".inp").append("<input type='text' data-pid='"+thisPid+"' data-id='"+newId+"' onblur = 'addData(this,3)' onclick='stopEvent() ' 	class = 'addInput' />")

	inputBj()
}

//	删除其他选项
function otherDel (modern,e){

	$(modern).parent(".name").empty().html("不包含").css({"background":"url('/ceca_two/img/wang3.png')no-repeat 80px center/14px 14px"});
	
	$(modern).parents(".hasBox").find(".para").find(".name").empty();
	$(modern).parents(".hasBox").find(".para").find(".inp").empty();
	
	window.event? window.event.cancelBubble = true : e.stopPropagation();
	
}


//	阻止冒泡事件
function stopEvent (e){
	window.event? window.event.cancelBubble = true : e.stopPropagation();
}

//	其他选项 储存数据


function addData (modern,name){
	
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	var thisid = $(modern).attr("data-id");
	var thisNode = treeObj.getNodeByParam("id",thisid);
	var thisHtml = $(modern).val();

	name == "1" ? thisNode.name = thisHtml : "" ;
	name == "2" ? thisNode.para[0].name = thisHtml : "" ;
	name == "3" ? thisNode.para[0].inp = thisHtml : "" ;

}

//	展示工程量单位

function selUnit (modern){
	$(modern).next("dl").empty();
	for (i = 0 ; i < shareUnit.length ; i++) {
		$(modern).next("dl").append("<dd onclick='choseUnit(this)' data-name ='"+shareUnit[i].name+"' >"+shareUnit[i].unit+"</dd>")
	}
	$(modern).next("dl").toggle();
}

//	选择 单位
function choseUnit (modern){
	
	var thisName = $(modern).attr("data-name"),
		thisUnit = $(modern).html();

	$(modern).parents(".eng").find(".name").html(thisName);
	$(modern).parents(".eng").find(".danwei").find("li").html(thisUnit);
	$(modern).parent().hide();
}

//	技术参数 下选框
function chooseThis (modern){

	$(modern).parent().prev().html($(modern).html())
	
}


//	获得下拉二级三级列表
function getList (modern,level){
	
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	
	var thisHtml = $(modern).html(),
		thisId = $(modern).attr("data-id"),
		thisPid = $(modern).attr("data-pId");
	
	var thisNode = treeObj.getNodeByParam("id",thisId);	//当前节点数据
	
	if (level == 2) {
		level = ".third" ;
		$(modern).parents(level).nextAll("div")
	}else if(level == 3){
		level = ".fouth" ;
	}

	$(modern).parent().parent().nextAll("div").empty();	//清空分部分项 
	$(modern).parents(".box").find(".para").find(".name").empty();	//清空分部分项 
	$(modern).parents(".box").find(".para").find(".danwei").empty();	//清空技术参数
	$(modern).parents(".box").find(".para").find(".inp").empty();	
//	
	$(modern).parent().prev(".name").attr({"data-id":thisId,"data-pId":thisPid,"title":thisHtml}).html(thisHtml);	//确认选择项
	
	if (thisNode.para) {	//技术参数

		for (i = 0 ; i < thisNode.para.length ; i++) {

			if (thisNode.para[i].children) {
				if (thisNode.para[i].unit && thisNode.para[i].unit != "undefined"  ) {
//			console.log(thisNode.para[i].unit)
					$(modern).parents(".box").find(".para").eq(i).find(".name").html(thisNode.para[i].name+"("+thisNode.para[i].unit+")");
				} else{

					$(modern).parents(".box").find(".para").eq(i).find(".name").html(thisNode.para[i].name);
				}
				
				$(modern).parents(".box").find(".para").eq(i).find(".inp").empty().append("<div onclick='showList(this)' style='text-align: center;width: 100%;height: 100%;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;'>请选择</div><div class='selectBox list2' style='position: relative;display: none;z-index: 999;border: 1px solid #F43838;background: white;'></div>")

				for (j = 0 ; j < thisNode.para[i].children.length ; j++) {
					$(modern).parents(".box").find(".para").eq(i).find(".selectBox").append("<li class='optionBox' onclick='chooseThis(this)' title='"+thisNode.para[i].children[j]+"'>"+thisNode.para[i].children[j]+"</li>")
				}
				
			} else{
				if (thisNode.para[i].unit && thisNode.para[i].unit != undefined ) {
					$(modern).parents(".box").find(".para").eq(i).find(".name").html(thisNode.para[i].name+"("+thisNode.para[i].unit+")");
					
				}else{
					$(modern).parents(".box").find(".para").eq(i).find(".name").html(thisNode.para[i].name);
				}

				$(modern).parents(".box").find(".para").eq(i).find(".inp").append("<input type='text' />");
			}
			
		}
	}
	if (thisNode.unit) {
		
		if (thisNode.unit == "—") {
			$(modern).parents(".box").find(".eng").find("input").css("background","none")
		}
		$(modern).parents(".box").find(".eng").find(".name").html(chooseUnit(thisNode.unit));
		
		$(modern).parents(".box").find(".eng").find(".danwei").find("li").html(thisNode.unit);
		
	}
	//工程量单位
	
	if (thisNode.children && thisNode.children.length > 0) {	// 渲染下拉列表
		
		$(modern).parents(".leve").find(level).append("<ul class='name' onclick='showList(this)'>不包含</ul><ul class='list'></ul>")
		for (i = 0 ; i < thisNode.children.length ; i++) {
			$(modern).parents(".leve").find(level).find(".list").append("<li onclick='getList(this,3)' data-id='"+thisNode.children[i].id+"'data-pId='"+thisNode.children[i].pId+"'>"+thisNode.children[i].name+"</li>")
		}
		
	}

	$(modern).parent().hide();

}

//	新增

function add (modern){
	
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	
	var thisPid = $(modern).parents(".box").find(".leve").find(".name").last().attr("data-pid") || $(modern).parents(".box").find(".leve").find("input").attr("data-pid");
	var thisName = $(modern).parents(".box").find(".leve").find(".name").html() || $(modern).parents(".box").find(".leve").find("input").val();
	var thisPNode = treeObj.getNodeByParam("id",thisPid);
	var thisId = thisPNode.children[thisPNode.children.length-1].id+1;
	


	$(modern).parents(".box").find(".leve").find(".name").last().attr("data-id",thisId) || $(modern).parents(".box").find(".leve").find("input").attr("data-id",thisId);
	
	var paraArr = [];
	
		for (i = 0 ; i < $(modern).parents(".hasBox").find(".para").length ; i++) {
			paraArr.push({
				name : $(modern).parents(".hasBox").find(".para").eq(i).find(".name").html(),
				inp : $(modern).parents(".hasBox").find(".para").eq(i).find(".inp").val() || $(modern).parents(".hasBox").find(".para").eq(i).find("div").eq(0).html()
			})
		}
	

	
	var newNode = {
		name : thisName ,
		id : Number(thisId),
		pId : Number(thisPid),
		para : paraArr
	};


	treeObj.addNodes(thisPNode,newNode);


	var copy = $(modern).parents(".box").clone();
    // 初始化格式
	copy.find(".second").find(".name").html("不包含").removeAttr("data-id").removeAttr("data-pId");
	copy.find(".third").empty();
	copy.find(".fouth").empty();
	copy.find(".para").find(".name").html("");
	copy.find(".para").find(".inp").empty();
	copy.find(".para").find(".danwei").html("");
	copy.find(".eng").find("input").val("");
	copy.find(".comp").find("input").val("");
	$(modern).parents(".box").after(copy);
	$(modern).next("span").html("删除")
	$(modern).replaceWith("<img src='/ceca_two/img/wang2.png'class='del1' onclick='del(this)' />");


	inputBj()
}
function add (modern){
	

	var copy = $(modern).parents(".box").clone();
    // 初始化格式
	copy.find(".second").find(".name").html("不包含").removeAttr("data-id").removeAttr("data-pId");
	copy.find(".third").empty();
	copy.find(".fouth").empty();
	copy.find(".para").find(".name").html("");
	copy.find(".para").find(".inp").empty();
	copy.find(".para").find(".danwei").html("");
	copy.find(".eng").find("input").val("");
	copy.find(".comp").find("input").val("");
	$(modern).parents(".box").after(copy);
	$(modern).next("span").html("删除")
	$(modern).replaceWith("<img src='/ceca_two/img/wang2.png'class='del1' onclick='del(this)' />");


	inputBj()
}
//	删除

function del (modern){
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	var thisId = $(modern).parents(".box").find(".leve").find(".name").last().attr("data-id");
	var thisPid = $(modern).parents(".box").find(".leve").find(".name").last().attr("data-pId");
	var thisPNode = treeObj.getNodeByParam("id",thisPid);
	var thisNode = treeObj.getNodeByParam("id",thisId);




//	treeObj.removeNode(nodes[i]);
	
//	for ( i = 0 ; i < thisNode.children.length ; i++) {
//		
//		thisNode.children[i].id == thisId ? thisNode.children.splice(i,1) :  '';
//		
//	}
	
	$(modern).parents(".box").remove();
	
}


// input 背景

function inputBj(){
	$("input").on("input ,onpropertychange",function (){
		if ( $(this).val() != "") {
			$(this).css("background","none")
		}else{
			$(this).css("background","url(/ceca_two/img/enter.png) center center no-repeat / 12px 12px")
		}
	})
}
inputBj()

// 下一步

$("#nextBtn").on("click",function (){
	
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");	

	for( i = 0 ; i < $(".hasBox").length ; i++ ){
		
		var thispId = $(".hasBox").eq(i).find(".leve").find("input").attr("data-pid") || $(".hasBox").eq(i).find(".leve").find(".name").last().attr("data-pid");
		var thisId = $(".hasBox").eq(i).find(".leve").find("input").attr("data-id") || $(".hasBox").eq(i).find(".leve").find(".name").last().attr("data-id");
		
		if (thispId != undefined) {
			
			var thisPNode = treeObj.getNodeByParam("id",thispId);
			var thisNode = treeObj.getNodeByParam("id",thisId);
			var paraArr = [];
			var arrName,arrInp;
			for (j = 0 ; j < thisNode.para.length ; j++) {
				
				if ($(".hasBox").eq(i).find(".para").eq(j).find(".name").find("input").length > 0) {
					arrName = $(".hasBox").eq(i).find(".para").eq(j).find(".name").find("input").val()
				}else{
					arrName = $(".hasBox").eq(i).find(".para").eq(j).find(".name").html();
				}
				
				if ($(".hasBox").eq(i).find(".para").eq(j).find(".inp").find("input").length > 0) {
					arrInp = $(".hasBox").eq(i).find(".para").eq(j).find(".inp").find("input").val()
				}else{
					arrInp = $(".hasBox").eq(i).find(".para").eq(j).find(".inp").find("div").first().html();
				}
				paraArr.push({
					name : arrName || "" ,
					inp : arrInp || ""
				})
			}
			var newNode = {
				pId : thispId ,
				name : "",
				gclName : $(".hasBox").eq(i).find(".eng").find(".name").html(),
				gclValue : $(".hasBox").eq(i).find(".eng").find("input").val(),
				gclUnit : $(".hasBox").eq(i).find(".eng").find(".danwei").find("li").html(),
				jscs :paraArr,
				zhhj : $(".hasBox").eq(i).find(".price").val()
			}
			if ($(".hasBox").eq(i).find(".leve").find("input").length > 0) {
				newNode.name = $(".hasBox").eq(i).find(".leve").find("input").val() ;
			}else{
				newNode.name = $(".hasBox").eq(i).find(".leve").find(".name").last().html();
			}
			console.log(newNode)
			treeObj.addNodes(thisPNode,newNode);
		}
		
	}
	
	
	for(i = 0 ; i < $(".hj").length ; i++){
		
		var thisId = $(".hj").eq(i).attr("data-id");
		var thisNode = treeObj.getNodeByParam("id",thisId);
		thisNode.bwhj = $(".hj").eq(i).find("input").val();
		
	}
	
	

	var allData = JSON.stringify(treeObj.getNodes());


	$.ajax({
		type:"post",
		url:"/guidance_service/front/costIndex/update.do",
		data:{
			costId : costId,
			features: allData
		},success : function (result){
			console.log(JSON.parse(allData))

//				window.location.href = "/ceca_two/modules/share/civil/three.html"+window.location.search ;

			
		},error : function (){
			alert(2)
		}
	});

})

// 返回上一步
$("#prevBtn").on("click",function (){
	window.location.href = "/ceca_two/modules/share/civil/one.html"+window.location.search;
})

// 点击任意地方隐藏下拉框
$(document).on("click",function (){
	$(".list").hide();
	$(".list2").hide();
})

//	得到url参数
function GetQueryString(name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

